﻿using CrmCustomizationsExcelAddIn.Helper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.Xrm.Sdk.Metadata;
using CrmCustomizationsExcelAddIn.Data;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using System.Linq;

namespace UnitTest
{
    
    
    /// <summary>
    ///This is a test class for RequestGeneratorHelperTest and is intended
    ///to contain all RequestGeneratorHelperTest Unit Tests
    ///</summary>
    ///
    [TestClass]
    public class RequestGeneratorHelperTest
    {
       
        private TestContext testContextInstance;
        private static IOrganizationService conn;
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            conn = RequestGeneratorTestHelper.connectOrg();
            
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for generateCrmOperationRequest
        ///</summary>
        [TestMethod()]
        [DeploymentItem("UnitTest\\UnitTestHelper\\AttributeToTest.xml")]
        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\AttributeToTest.xml", "Attribute", DataAccessMethod.Sequential)]
        public void generateCrmOperationRequestTest()
        {
            string input = ((string)TestContext.DataRow["Input"]);
            string expresult = ((string)TestContext.DataRow["Result"]);
            int   lanaguage = int.Parse(((string)TestContext.DataRow["langauge"]));
            string orgprefix = ((string)TestContext.DataRow["orgprefix"]);
            string entity = ((string)TestContext.DataRow["entity"]);
            CrmOperationHelper opHelper = new CrmOperationHelper();
            opHelper.Service = conn;
            EntityMetadata filteredMetadata = opHelper.RetriveEntityAtrribute(entity);
            OptionSetMetadata  [] option = new OptionSetMetadata[3];
            GlobalApplicationData.Instance.currentLanguage = 1033;
            AttributeExcelSheetsInfo excelsheet = new AttributeExcelSheetsInfo(filteredMetadata, orgprefix);
            AttributeRequestGenerator target = new AttributeRequestGenerator(excelsheet,  option); 
            ExcelMatrix dataMatrix = RequestGeneratorTestHelper.generateRandomExcelMatrix(input,2);
            IEnumerable<CrmOperation> oplist =  target.generateCrmOperationRequest(dataMatrix);
            RequestGeneratorTestHelper.exceuteOperaion(opHelper, oplist);
            EntityMetadata etm =   opHelper.RetriveEntityAtrribute(entity);
            AttributeMetadata attr = (etm.Attributes.Where(x => x.SchemaName == expresult.Split('|')[2])).First();
            Assert.AreEqual(expresult,RequestGeneratorTestHelper.getAttributePropertyString(attr, lanaguage, etm.ManyToOneRelationships));
        }

    }

}
